package com.witframework.web.tag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class AjaxUploadTag extends BodyTagSupport{
  private static final String AJAXUPLOADTAG = "jxpldtgcj2006";
  private String name = null;
  private String action = null;
  private String onSubmit = null;
  private String beforeAction = null;
  private String afterAction = null;
  private String id = null;
  private String sBody = null;

  public String getName()
  {
    return this.name;
  }

  public void setName(String paramString)
  {
    this.name = paramString;
  }

  public void setAction(String paramString)
  {
    this.action = paramString;
  }

  public String getAction()
  {
    return this.action;
  }

  public void setOnSubmit(String paramString)
  {
    this.onSubmit = paramString;
  }

  public String getOnSubmit()
  {
    return this.onSubmit;
  }

  public void setBeforeAction(String paramString)
  {
    this.beforeAction = paramString;
  }

  public String getBeforeAction()
  {
    return this.beforeAction;
  }

  public void setAfterAction(String paramString)
  {
    this.afterAction = paramString;
  }

  public String getAfterAction()
  {
    return this.afterAction;
  }

  public int doAfterBody()
    throws JspException
  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent == null)
    {
      this.sBody = "";
    }
    else
    {
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
    }
    this.sBody = this.sBody.trim();
    return 0;
  }

  public int doEndTag()
    throws JspException
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    if (this.id == null)
    {
      Integer localInteger = (Integer)this.pageContext.getAttribute("jxpldtgcj2006", 1);
      if (localInteger == null)
        localInteger = new Integer(0);
      this.id = "jxpldtgcj2006_" + localInteger;
      this.pageContext.setAttribute("jxpldtgcj2006", new Integer(localInteger.intValue() + 1), 1);
    }
    if (this.afterAction != null)
    {
      localStringBuffer.append("<script language=\"JavaScript\" type=\"text/javascript\">\n");
      localStringBuffer.append(" var ");
      localStringBuffer.append(this.id + "_v=0;\n");
      localStringBuffer.append("</script>\n");
    }
    localStringBuffer.append("<iframe name=\"");
    localStringBuffer.append(this.id + "_f\"");
    if (this.afterAction != null)
    {
      localStringBuffer.append(" onLoad=\"");
      localStringBuffer.append("if (" + this.id + "_v>0) {");
      localStringBuffer.append(this.afterAction + "();" + this.id + "_v=0;");
      localStringBuffer.append("}\"");
    }
    localStringBuffer.append(" style=\"display:none\" width=\"0\" height=\"0\"></iframe>");
    localStringBuffer.append("<form id=\"" + this.id + "\"");
    if (this.name != null)
    {
      localStringBuffer.append(" name=\"");
      localStringBuffer.append(this.name);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" method=\"post\" enctype=\"multipart/form-data\"");
    if (this.action != null)
    {
      localStringBuffer.append(" action=\"");
      localStringBuffer.append(this.action);
      localStringBuffer.append("\"");
    }
    if ((this.onSubmit != null) || (this.beforeAction != null) || (this.afterAction != null))
    {
      localStringBuffer.append(" onSubmit=\"");
      if (this.onSubmit != null)
        localStringBuffer.append("if (" + this.onSubmit + "()){");
      if (this.afterAction != null)
        localStringBuffer.append(this.id + "_v=1;");
      if (this.beforeAction != null)
        localStringBuffer.append(this.beforeAction + "();");
      localStringBuffer.append("submit();");
      if (this.onSubmit != null)
        localStringBuffer.append("}");
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" target=\"");
    localStringBuffer.append(this.id + "_f\"");
    localStringBuffer.append(">");
    localStringBuffer.append(this.sBody);
    localStringBuffer.append("</form>");
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("AjaxUpload: could not write data");
    }
    dropData();
    return 6;
  }

  public void release()
  {
    dropData();
  }

  private void dropData()
  {
    this.name = null;
    this.action = null;
    this.onSubmit = null;
    this.beforeAction = null;
    this.afterAction = null;
    this.id = null;
    this.sBody = null;
  }
}